function resetearFormulario(form) {
	$(form).find(':input').each(function() {
		switch (this.type) {
		case 'password':
			$(this).val('');
			break;
		case 'select-multiple':
			$(this).val('');
			break;
		case 'select-one':
			$(this).children("option[selected]").removeAttr("selected");
			$(this).children("option[index=0]").attr("selected", "selected");
			break;
		case 'text':
			$(this).val('');
			break;
		case 'textarea':
			$(this).val('');
			break;
		case 'checkbox':
			$(this).attr("checked", false);
			break;
		case 'radio':
			$(this).attr("checked", false);
		}
	});
}

function limpiarFila(id) {
	for ( var i = 1; i <= cols; i++)
		$("#col" + i + "_" + id).html("");
}

function actualizar(obj) {
	$.get($(obj).attr("href"), function(responseText, status, xmlHttpRequest) {
		$("#divDT").html($("#divDT", responseText).html());
	});
}

function bloquearFormulario(form) {
	$(form + " input, select").attr("disabled", "true");
}

function desbloquearFormulario(form) {
	$(form + " input, select").removeAttr("disabled");
}

function acentos(string) {
	alert(string.search(/[\d\w\sáéíóú]+/));
	if (string.search(/[\d\w\sáéíóú]+/) >= 0) {
		string = string.replace(/á/g, "&aacute;");
		string = string.replace(/é/g, "&eacute;");
		string = string.replace(/í/g, "&iacute;");
		string = string.replace(/ó/g, "&oacute;");
		string = string.replace(/ú/g, "&uacute;");
	}
	return string;
}
function udplist(url, id, idSelect, num) {
	$.getJSON(url, {
		id : id
	}, function(data) {
		var select = $("#" + idSelect);
		var options = select.attr("options");
		$("option", select).remove();

		$.each(data, function(key, value) {
			options[options.length] = new Option(value, key);
		});
		select.val(num);
	}, "json");
}
function verElemento(num, form) {
	desbloquearFormulario(form);
	resetearFormulario(form);
	obtenerDatos(num);
	bloquearFormulario(form);
	$("#dialog").dialog("open");
	$("#grabar").hide();
}
function editarElemento(num, form) {
	desbloquearFormulario(form);
	resetearFormulario(form);
	obtenerDatos(num);
	$("#dialog").dialog("open");
	$("#grabar").show();
}
function udtElement(obj) {
	var num = $("#rowNum").val();
	$.post(URL + "accion/ajax/" + URLAccion + "/grabar", obj, function(data) {
		udtRow(num, data);
		$("#dialog").dialog("close");
	}, "json");
}
function insertElement(obj) {
	$.post(URL + "accion/ajax/" + URLAccion + "/insertar", obj, function(data) {
		$("#dialog").dialog("close");
		var lastRow = $("table tr:last").attr("class").split("_")[1]++;
		if (isNaN(lastRow))
			lastRow = 1;
		$("#list").append(addRow(lastRow, data));
	}, "json");
}
function grabar() {
	var varId = $("#id").val();
	var obj = seleccionarElemento(null);
	if (varId != null && varId != '') {
		udtElement(obj);
	} else {
		insertElement(obj);
	}
}
function borrarElemento(num) {
	if (confirm("¿Seguro que quieres borrarlo?")) {
		var obj = seleccionarElemento(num);
		$.post(URL + "accion/ajax/" + URLAccion + "/borrar", obj,
				function(data) {
					$("#dialog").dialog("close");
					actualizar($(location));
				}, "json");
	}
}

function add(form) {
	desbloquearFormulario(form);
	resetearFormulario(form);
	$("#id").val("");
	$("#grabar").show();
	$("#dialog").dialog("open");
}

function updbottons(psitio, cols) {
	var tr = $("<tr class=\"trudp\"><td align=\"right\" colspan=\"" + cols
			+ "\">" + "<a href=\"#\" id=\"grabarudp\" class=\"grabar\">Grabar</a>&nbsp;"
			+ "<a href=\"#\" id=\"cancelarudp\" class=\"cancelar\">Cancelar</a>"
			+ "<input type=\"hidden\" id=\"idudp\" name=\"idudp\" /></td></tr>");
	$(psitio).after(tr);
	
	$("#cancelarudp").button({
		text: false,
		icons: {
			primary: 'ui-icon-cancel'
		}
	});
	
	$("#grabarudp").button({
		text: false,
		icons: {
			primary: 'ui-icon-disk'
		}
	});
}
function newinput(psitio, pid, ptype, pvalue) {
	if (ptype == "checkbox") {
		$("<input />", {
			id : pid,
			name : pid,
			type : ptype,
			value : "true"
		}).appendTo(psitio);
		if(pvalue)
			$("#" + pid).attr('checked', 'checked');
	} else {
		$("<input />", {
			id : pid,
			name : pid,
			type : ptype,
			value : pvalue
		}).appendTo(psitio);

	}
}

function lista(obj, value, url){
	$("#" + obj).autocomplete({
		source: function( request, response ) {
			$.getJSON(url, {
				input : request.term, 
				max : 7 
			}, function(data) {
				response( $.map( data, function( item ) {
					return {
						label: item.nombre,
						value: item.id
					}
				}));
			});
		},
		minLength: 2,
		select: function( event, ui ) {
			$("#" + obj).val(ui.item.label);
			$("#" + value).val(ui.item.value);
			return false;
		},
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});
}
	
function newobject(psitio, pos, obj) {
	var tr = $(getFila(obj));
	if (pos == 'A')
		$(psitio).before(tr);
	else if (pos == 'D')
		$(psitio).after(tr);
}

function doTimer() {
	if (!timer_is_on) {
		timer_is_on = 1;
		consulta();
	}
}

function stopCount() {
	clearTimeout(t);
	timer_is_on = 0;
}

$(document).ready(function() {
	$(".nuevo").button({
		text: false,
		icons: {
			primary: 'ui-icon-plusthick'
		}
	});
	
	$(".cancelar").button({
		text: false,
		icons: {
			primary: 'ui-icon-cancel'
		}
	});
	
	$(".grabar").button({
		text: false,
		icons: {
			primary: 'ui-icon-disk'
		}
	});
	
	$(".logout").button({
		text: true,
		icons: {
			primary: 'ui-icon-power'
		}
	});
	
	$(".volver").button({
		text: true,
		icons: {
			primary: 'ui-icon-arrowreturnthick-1-w'
		}
	});
});